﻿using Niusys.Entities;
using Niusys.Repostories;
using Niusys.WebAPI.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Niusys.WebAPI.Common.Services
{
    public interface IAuthenticationService
    {
        UserDevice GetUserDevice(string sessionKey);
        User GetUser(Guid userId);
        void UpdateUserDevice(UserDevice userSession);
        User GetUserByLoginName(string loginId);
        UserDevice GetUserDevice(Guid userId, int deviceType);
        void AddUserDevice(UserDevice existsDevice);
    }

    public class AuthenticationService : IAuthenticationService
    {
        private readonly IAuthenticationRepository _authenticationRepository;
        private readonly IUserRepository _userRepository;

        public AuthenticationService(IAuthenticationRepository authenticationRepository, IUserRepository userRepository)
        {
            this._authenticationRepository = authenticationRepository;
            this._userRepository = userRepository;
        }

        public void AddUserDevice(UserDevice userDevice)
        {
            _authenticationRepository.AddUserDevice(userDevice);
        }

        public User GetUser(Guid userId)
        {
            return _userRepository.GetUser(userId);
        }

        public User GetUserByLoginName(string loginName)
        {
            return _userRepository.GetUserByLoginName(loginName);
        }

        public UserDevice GetUserDevice(string sessionKey)
        {
            return _authenticationRepository.GetUserDevice(sessionKey);
        }

        public UserDevice GetUserDevice(Guid userId, int deviceType)
        {
            return _authenticationRepository.GetUserDevice(userId, deviceType);
        }

        public void UpdateUserDevice(UserDevice userDevice)
        {
            _authenticationRepository.UpdateUserDevice(userDevice);
        }
    }
}
